package com.dm.cloud.auth.auth2;

import cn.dev33.satoken.oauth2.logic.SaOAuth2Template;
import cn.dev33.satoken.oauth2.model.SaClientModel;
import cn.hutool.core.util.ObjUtil;
import com.dm.cloud.auth.common.po.AuthClientDetails;
import com.dm.cloud.auth.mapper.AuthClientDetailsMapper;
import com.dm.cloud.auth.service.AuthClientDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Sa-Token OAuth2.0 整合实现
 */
@Component
public class SaOAuth2TemplateImpl extends SaOAuth2Template {

    @Autowired
    private AuthClientDetailsService authClientDetailsService;

    // 根据 id 获取 Client 信息
    @Override
    public SaClientModel getClientModel(String clientId) {
        AuthClientDetails clientDetail = authClientDetailsService.getById(clientId);
        if(clientDetail!=null && ObjUtil.isNotEmpty(clientDetail)) {
            return new SaClientModel()
                    .setClientId(clientDetail.getClientId())
                    .setClientSecret(clientDetail.getClientSecret())
                    .setAllowUrl(clientDetail.getAllowUrl())
                    .setContractScope(clientDetail.getScope())
                    .setIsAutoMode(true);
        }
        return null;
    }

    // 根据ClientId 和 LoginId 获取openid
    @Override
    public String getOpenid(String clientId, Object loginId) {
        // 此为模拟数据，真实环境需要从数据库查询
        return "";
    }

}
